﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tracker.Models.BLL;
using tracker.Models;
using System.Data;
using tracker.Models.DTO;

namespace tracker.Controllers
{
    public class HandlerSearchOdometersController : Controller
    {
       /// <summary>
       /// Get All Driver Info
       /// </summary>
       /// <returns></returns>
        [CallAjaxFilter]
        public JsonStringResult Index(string trackName, string fromtime, string totime)
        {
            string result = "{[]}";
            string _username = HttpContext.Session[Common.USER].ToString();
            string _language = HttpContext.Session[Common.LANGUAGE].ToString();
            string timezone = HttpContext.Session[Common.TIMEZONE].ToString();
            _language = _language == string.Empty ? "vi" : _language;

            List<ENTTrackerName> listTrackerName = new List<ENTTrackerName>();
            listTrackerName = HandlerSearchTrackerNameController.getListDeviceByUser(_username);

            string username = "";
            foreach (ENTTrackerName item in listTrackerName)
            {
                username += item.username + ",";
            }
            username = username.Remove(username.LastIndexOf(","));

            List<ENTsearchOdometers> list = new List<ENTsearchOdometers>();

            List<string> listDevice = new List<string>();

            if (trackName.Length == 0)
            {
                foreach (ENTTrackerName item in listTrackerName)
                {
                    listDevice.Add(item.trackname);
                }
            }
            else
            {
                string[] listdv = trackName.Split(',');
                foreach (string item in listdv)
                {
                    listDevice.Add(item);
                }
            }

            foreach (string item in listDevice)
            {
                DataTable dtDataToTime = TrackLogBLL.GetJourneyToTime(new string[]{"totime",
                                                                                   "UserName",
                                                                                   "TrackName"
                                                                      },
                                                                      new object[]{ClassCommon.gpsExtensionMethod.convertGMTServerTime(DateTime.Parse(totime),timezone),
                                                                                   username,
                                                                                   item
                                                                      }).Tables[0];
                DataTable dtDataFromTime = TrackLogBLL.GetJourneyFromTime(new string[]{"fromtime",
                                                                                       "UserName",
                                                                                       "TrackName"
                                                                          },
                                                                          new object[]{ClassCommon.gpsExtensionMethod.convertGMTServerTime(DateTime.Parse(fromtime),timezone),
                                                                                       username,
                                                                                       item
                                                                          }).Tables[0];

                if (dtDataFromTime.Rows.Count > 0 && dtDataToTime.Rows.Count > 0)
                {
                    ENTsearchOdometers ent = new ENTsearchOdometers();

                    //DeviceName,Journey,Gps_time 
                    ent.beginodometers = dtDataFromTime.Rows[0]["Journey"].ToString();
                    ent.begintime = ClassCommon.gpsExtensionMethod.convertGMTClientTime(DateTime.Parse(dtDataFromTime.Rows[0]["RecieveTime"].ToString()), timezone).ToString("yyyy-MM-dd hh:mm:ss tt");
                    ent.trackername = item;
                    ent.sssid = dtDataFromTime.Rows[0]["DeviceKeySecurity"].ToString();
                    ent.endodometers = dtDataToTime.Rows[0]["Journey"].ToString();
                    ent.endtime = ClassCommon.gpsExtensionMethod.convertGMTClientTime(DateTime.Parse(dtDataFromTime.Rows[0]["RecieveTime"].ToString()), timezone).ToString("yyyy-MM-dd hh:mm:ss tt");
                    list.Add(ent);
                }
                dtDataToTime.Dispose();
                dtDataFromTime.Dispose();
            }

            if (list.Count > 0)
            {
                result = Utilities.getJsonString(list, "totalCount:" + list.Count + ",data:", 1);
            }

            return new JsonStringResult(result);
        }
    }
}
